from django.shortcuts import render
from django.http import HttpResponse

from learn import models, AutoDeploy

# Create your views here.
def index(request):
    pass
    return render(request, 'learn/index.html')

def login(request):

    testinfos = []

    for index in range(10):
        testinfo = models.TestInfo()

        testinfo.firstName = str(index) + 'firstName'
        testinfo.LastName = str(index) + 'LastName'
        testinfo.UserName = str(index) + 'UserName'

        testinfos.append(testinfo)

    return render(request, 'learn/login.html', {'testinfos': testinfos})
    #return render(request, 'learn/login.html')

def update(request):

    raw_data = request.POST.get('exampleInputAmount', None)

    if (raw_data == 'update'):

        testinfos = []

        for index in range(30, 40):
            testinfo = models.TestInfo()

            testinfo.firstName = str(index) + 'firstName'
            testinfo.LastName = str(index) + 'LastName'
            testinfo.UserName = str(index) + 'UserName'

            testinfos.append(testinfo)

        return render(request, 'learn/login.html', {'testinfos': testinfos})
    else:
        from django.shortcuts import redirect
        return redirect("/login/")

def autoDeploy(request):
    raw_data = request.POST.get('jiraConns', None)

    jiraList = str(raw_data).split(',')

    username = 'cszhangyi'
    password = 'Zhang1987wing'

    autoDeploy = AutoDeploy(username, password)

    autoDeploy.loginVirtualJenkins()
    autoDeploy.loginDockerJenkins()
    autoDeploy.loginJira()

    for i in jiraList:
        mainIssue = models.mainIssue
        mainIssue = autoDeploy.operateMainIssue(i)

        if (mainIssue.errorMsg != ''):
            return render(request, 'learn/login.html', {'mainIssue': mainIssue})
        else:
            autoDeploy.getSubTasks(mainIssue.jiraMainIssue, isRetry=False, isStgSuccess=False, branch='master')